cd "S:\Katja_Aksel\JFQA index paper\v3\pseudo_data"
clear
set more 1
cap log close

log using ./logs/1_investortypes.log, replace

****CATEGORIZE INVESTORS IN ALL FIRMS
*****identify repeat angels
use ./2c_all_purchases.dta, clear
*cleaning up the data by dropping tiny investments
drop if amount<10000
drop if shares<2
drop if (amount/shares)<1
*keep only transactions with substantial ownership and where other investors would have had a chance to sell
drop if ownership<0.01
drop if ownership>0.99
**keep only investments in HIP firms
keep if sample==1
keep if akt_sy_type=="P"
keep orgnr investor_id
duplicates drop
order investor_id
sort investor_id orgnr
bys investor_id: egen investments=count(orgnr)
keep investor_id investments
duplicates drop
tab investments
keep if investments>1
gen repeatangel=1
drop investments
save ./repeat_angels.dta, replace

****identify founders/entrepreneurs from the incorporation file - those are people being with the firm from the beginning
use ./04c_b_keyperson_withids.dta, clear
keep orgnr investor_id role
duplicates drop
drop if investor_id==.
drop role
gen entrepreneur=1
save ./entrepreneurs.dta, replace

***identify family members in the firms
use ./new_related_investors.dta, clear
keep orgnr investor_id
duplicates drop
gen family=1

merge 1:m orgnr investor_id using ./2c_all_purchases.dta
drop if _merge==1
replace family=0 if _merge==2
drop _merge

***add repeat angels
merge m:1 investor_id using ./repeat_angels.dta
replace repeatangel=0 if _merge==1
drop _merge

***add entrepreneurs of the firms
merge m:1 orgnr investor_id using ./entrepreneurs.dta
drop if _merge==2
replace entrepreneur=0 if _merge==1
drop _merge

order orgnr investor_id entrepreneur family repeat

gen investortype=1 if entrepreneur==1
replace investortype=2 if investortype==. & family==1
replace investortype=3 if investortype==. & repeatangel==1
replace investortype=4 if investortype==. & akt_sy_type=="P"
replace investortype=5 if investortype==. & vcinvestor==1
replace investortype=6 if investortype==. & akt_sy_type=="V"
replace investortype=7 if investortype==. & akt_sy_type=="K"
replace investortype=8 if investortype==.
*23,360 unidentified transactions
label variable investortype "1entrepreneur 2family 3repeatangel 4ind 5VC 6corp 7fin 8other"
tab investortype

save ./2d_all_purchases.dta, replace

